<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>产品管理</title>
		<meta charset="utf-8">
		<title>Layui</title>
		<meta name="renderer" content="webkit">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
		<script src="../../layui/layui.js" charset="utf-8"></script>
		<script src="../../js/jquery3.js" type="text/javascript" charset="utf-8"></script>

		<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
	</head>

	<style type="text/css">
		body{
			overflow-y: hidden;
		}
		#dv {
			background-color: white;
			width: 96%;
			margin: 20px 20px auto;
		}
		.add_tb {
			width: 26px;
			height: 26px;
			margin: 0 0 0 10px;
		}

	</style>
	<body bgcolor="whitesmoke">

		<div id="dv">
			</br>
			&nbsp;&nbsp;&nbsp;&nbsp;<i class="layui-icon layui-icon-cart-simple" style="width: 150px;height: 100px;"><b>产品管理</b></i>
			</br></br>
		</div>
		<table class="layui-hide" id="test" lay-filter="test"></table>

		<div id="toolBar" style="display: none;">
			<div class="layui-btn-group">
				<button type="button" class="add_tb " lay-event="add"><i class="layui-icon">&#xe654;</i></button>
			</div>
		</div>

		<!--每一行后面的删除和修改按钮-->
		<div style="display: none" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="singleEdit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="singleDel">删除</a>
		</div>

		<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

		<script>
			$(function() {
				$.post("/user/getSelf", {}, function(data) {
					if (data != null) {
						layui.use('table', function() {
							var table = layui.table;
							var layer = layui.layer;
							table.render({
								elem: '#test',
								url: '/product/findAll',
								method: 'post',
								page: {
									limits: [5, 10, 15, 20],
									limit: 5,
									theme: '#5FB878',
								},

								toolbar: '#toolBar' //开启头部工具栏，并为其绑定左侧模板
									,
								defaultToolbar: ['filter', 'exports'],
								title: '订单数据表',
								cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
									,
								cols: [
									[{
											title: '序号',
											type: "numbers",
										},
										{
											field: 'prId',
											title: 'id',
											fixed: 'left',
											hide: true
										}, {
											field: 'prName',
											title: '产品名称'
										}, {
											field: 'prNum',
											title: '存量'
										}, {
											field: 'prPrice',
											title: '单价'
										}, {
											field: 'prStatus',
											title: '产品状态'
										},
										{
											field: 'prDescribe',
											title: '产品描述'
										}, {
											field: 'prCreatDate',
											title: '创建时间'
										}, {
											field: 'prUpdateDate',
											title: '修改时间'
										},
										{
											field: 'prEndPerson',
											title: "最后操作人"
										}, {
											fixed: 'right',
											title: '操作',
											toolbar: '#barDemo',
											width: 150
										}]
								],
							});
							
							
							//监听每一行的操作
							table.on('tool(test)', function(obj) {
								var data2 = obj.data;

								if (obj.event == "singleEdit") {
									layer.open({
										type:2,
										title:'修改信息',
										shadeClose:true,
										shade:0.8,
										area:['700px','80%'],
										shadeClose:false,
										content:'update_products.html?prId='+data2.prId+"&uid="+data.u_id
									});
								}else if(obj.event =="singleDel"){
									layer.confirm("真的删除吗？",function(index){
										$.post("/product/delProduct",{"prId":data2.prId},function(res){
											if (res=="success") {

												layer.msg('删除成功', {icon: 1});
												obj.del();
												layer.close(index);
											}
										})
									})
								}
							});
							
							//头工具栏事件
							table.on('toolbar(test)', function(obj) {
								switch (obj.event) {
									case 'add':
										layer.open({
											type:2,
											title:'添加信息',
											shadeClose:true,
											shade:0.8,
											area:['700px','600px'],
											shadeClose:false,
											content:'add_products.html?uid='+data.u_id
											
										})
										break;
								}								
							});


                });
            } else {
                alert("未登录，请先登录")
                location.href = "../login/login.html"
            }
        })


			})
		</script>
	</body>
</html>
